<?php

$search = $_GET['searWords'];



if(!empty($_GET['searWords'])){

	$data = utils::gpc_check($_GET);

	/* only if REGEXP used for search....

	if(strpos($data['searWords'], ' ') !== FALSE) {

		$data['searWords'] = '(\b'.str_replace(' ','\b)|(\b',$data['searWords']).'\b)';

	}

	*/

	$data['searWords'] = trim($data['searWords']);

	if (strlen($data['searWords']) < 2) {

		echo "Please insert at least 2 characters";

	} else {

		if (isset($data['page'])) {

			$page = $data['page'];

		} else {

			$page = 1;

		}

		$start = ($page-1)*20;

		//$res = mysql_query("SELECT file_name,file_title FROM bj_audio WHERE file_title  REGEXP('{$data['searWords']}') ORDER BY file_name") or die(mysql_error());

		//$res = mysql_query("SELECT file_name,file_title FROM bj_audio WHERE MATCH(file_name) AGAINST ('*{$data['searWords']}*' IN BOOLEAN MODE) ORDER BY file_name");

		/*

		$res = mysql_query("SELECT count(id_file) as nr FROM bj_audio WHERE MATCH(file_name) AGAINST ('*{$data['searWords']}*' IN BOOLEAN MODE) ORDER BY file_name");

		if ($res !== FALSE && mysql_num_rows($res)) {

			while ($lin = mysql_fetch_assoc($res)) {

				$rows = $lin['nr'];

			}

		}

		*/

		/*

		$like = true;

		$searchWords = explode(' ', $data['searWords']);

		foreach ($searchWords as $word) {

			if (strlen($word) > 2) $like = false;

		}

		*/

		//if (strlen($data['searWords']) == 2) {

		//if ($like) {

			/*$query = "SELECT 

				file_name, 

				file_title, 

				max(MATCH(file_name) AGAINST ('*{$data['searWords']}*')) as relevance 

				FROM bj_audio 

				where file_name like '%{$data['searWords']}%' 

				or MATCH(file_name) AGAINST ('*{$data['searWords']}*' in boolean mode) 

				group by file_name 

				order by relevance desc";*/

			/*

			$query = "SELECT 

				file_name, 

				file_title, 

				MATCH(file_name) AGAINST ('*{$data['searWords']}*' in boolean mode) as relevance 

				FROM bj_audio 

				where file_name like '%{$data['searWords']}%' 

				or MATCH(file_name) AGAINST ('*{$data['searWords']}*' in boolean mode) 

				group by file_name 

				order by relevance asc";

			*/

		//} else {

			/*

			$query = "SELECT 

				file_name, 

				file_title, 

				MATCH(file_name) AGAINST ('*{$data['searWords']}*') as relevance 

				FROM bj_audio 

				WHERE MATCH(file_name) AGAINST ('*{$data['searWords']}*' in boolean mode) having relevance > 0 

				order by relevance desc, file_name";

			*/

		//}

		

		$query = "

select * from ( 

(SELECT file_name, file_title, MATCH(file_name) AGAINST ('*{$data['searWords']}*') as relevance FROM bj_audio WHERE MATCH(file_name) AGAINST ('*{$data['searWords']}*' in boolean mode) having relevance > 0 order by relevance desc, file_name) 

union 

(SELECT file_name, file_title,MATCH(file_name) AGAINST ('*{{$data['searWords']}}*' in boolean mode) as relevance FROM bj_audio where file_name like '%{$data['searWords']}%' group by file_name)) as serchresult group by file_name order by relevance desc

		";

//		echo $query . "<br>";

		$res = mysql_query($query);

		if ($res !== FALSE && mysql_num_rows($res)) {

			$rows = mysql_num_rows($res);

		}

		$pages = ceil($rows/20);

		//$res = mysql_query("SELECT file_name,file_title FROM bj_audio WHERE MATCH(file_name) AGAINST ('*{$data['searWords']}*' IN BOOLEAN MODE) ORDER BY file_name LIMIT ".$start.",20");

		$query .= " LIMIT ".$start.",20";

		$res = mysql_query($query);

		if ($res !== FALSE && mysql_num_rows($res)) {

			$cnt = 1 + ($page-1)*20;

			while ($lin = mysql_fetch_assoc($res)) {

				echo $cnt . '.&nbsp;<a href="' . utils::create_href($lin['file_title']) .'">' . str_replace('.mp3', '', $lin['file_name']) . '</a><br>';

				$cnt ++;

			}

			mysql_free_result($res);

		} else {

			echo "<b> There are no results matching your keywords.<br>Please try again.</b>";

		}

		if ($pages > 1) {

			if ($page > 1) {

				echo '<a href="'.$ABS_URL.'search.php?searWords='.$data['searWords'].'&search=Search&page='.($page-1).'"><b>Previous</b> | </a> ';

			}

			if ($page < $pages) {

				echo '<a href="'.$ABS_URL.'search.php?searWords='.$data['searWords'].'&search=Search&page='.($page+1).'"><b>Next</b></a>';

			}

		}

	}



  /*

if ($handle = opendir('./themesongs')) {

	$count = 1;

    while (false !== ($file = readdir($handle))) {

        if(strstr($file, ".mp3")) {

			if(strstr(strtolower($file), strtolower($search))) {

				echo $count.'. <a href="player.php?song='.$file.'">';

				$theFile = str_replace($search, "<font color=#cc0033 face=Arial>".$search."</font>", $file);

				$theFile = str_replace(".mp3", "", $theFile);

				echo "$theFile";

				echo "</a><br><br>";

				$count++;

			}	

    	}

	}

    closedir($handle);

}

*/

}//if empty searWords

?>



